package com.liugai.lgeachday.exception;

import com.liugai.lgeachday.request.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    private final static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 判断错误是否是已定义的已知错误，不是则由未知错误代替，同时记录在log中
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Response exceptionGet(Exception e){
        if(e instanceof GlobalException){
            GlobalException MyException = (GlobalException) e;
            return new Response(MyException.getCode(),MyException.getMessage(), null);
        }
        LOGGER.error(new Response(400101,null, null).getMsg(),e);
        return new Response(400101,null, null);
    }
}
